#!/usr/bin/env python
# coding=utf-8

from pymongo import MongoClient
import tushare as ts
import numpy as np
import pandas as pd
import json

# 获取所有股票数据
# 用于构造数据库

def get_db():
    client = MongoClient("localhost", 27017)
    db = client.stock
    return db

if __name__ == "__main__":
    db = get_db()
    print(db)

    with open("stock_list.txt") as f:
        for line in f:
            line = line.strip()
            if line == '':
                continue
            
            # tushare api, 获取数据(可以打印下看看数据格式是怎样的)
            origin_data = ts.get_k_data(line,  ktype='D', start='2005-01-01')
            if origin_data.empty:
                continue
            # origin_data['date'] = origin_data.index
            # origin_data['id'] = line
            # print origin_data.dtypes
            # records = json.loads(origin_data.T.to_json()).values()
            db.stocks.insert(json.loads(origin_data.to_json(orient='records', date_format='iso')))

            print(line)